﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.JinShengService;
using System.Collections.ObjectModel;
using EHR.Common;
using CommunitySystem.EmpbasicService;

namespace CommunitySystem.PageView
{
    public partial class AddJinshengControl : UserControl
    {
        public ObservableCollection<JinShen_MainInfo> entityList = new ObservableCollection<JinShen_MainInfo>();
        private JinShen_MainInfo entity = new JinShen_MainInfo();

        private EmpbasicService.EmpBasicServiceClient empclient = new EmpbasicService.EmpBasicServiceClient();

        public AddJinshengControl(JinShen_MainInfo main, ObservableCollection<JinShen_MainInfo> list,int orgID)
        {
            InitializeComponent();
            if (orgID>0)
            {
                empclient.GetBianZhiModelCompleted += new EventHandler<GetBianZhiModelCompletedEventArgs>(empclient_GetBianZhiModelCompleted);
                empclient.GetBianZhiModelAsync(orgID.ToString(),0);
            }

            entity = main;
            entity.PositionLevel = "乡科级正职或副职";
            this.bjs.DataContext = main;
            entityList = list;

            //每个人登录进来所管辖的单位不同
            dtbOrg.Parameter = new string[1] { "loginname=" + Session.LoginUser.LoginName };
            dtbOrg.Field = "OrgName";
        }

        void empclient_GetBianZhiModelCompleted(object sender, GetBianZhiModelCompletedEventArgs e)
        {
            biaoti.ItemsSource = null;
            biaoti.ItemsSource = e.Result;
            if (biaoti.ItemsSource != null)
            {
                for (int i = 0; i < ((ObservableCollection<BianZhiModel>)biaoti.ItemsSource).Count; i++)
                {
                    if (((ObservableCollection<BianZhiModel>)biaoti.ItemsSource)[i].JiGouMingCheng == entity.Department)
                        biaoti.SelectedIndex = i;
                }
            }
        }

        private void dtbOrg_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("SelectItem") && dtbOrg.SelectItem != null)
            {
                entity.OrgID = dtbOrg.SelectItem.Id;
                
            }
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var l = entityList.Count;
            if (l < 2)
            {
                MessageBox.Show("不可再移除！", "系统提示", MessageBoxButton.OK);
                return;
            }
           else {
                this.Visibility = Visibility.Collapsed;
                entityList.Remove(entity);
            }
        }
        private void epJbxx_Collapsed(object sender, RoutedEventArgs e)
        {
            if (epJbxx != null)
            {
                if (epJbxx.IsExpanded == false) bjs.Visibility = Visibility.Visible;
            }
        }

        private void epJbxx_Expanded(object sender, RoutedEventArgs e)
        {
            if (epJbxx != null)
            {
                if (epJbxx.IsExpanded == true) bjs.Visibility = Visibility.Collapsed;
            }
        }

        private void tbDepart_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.epJbxx.Header = _Department + "【" + zw + "】";
        }
        string zw = "";
        private void dtbPosition_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("SelectItem") && dtbPosition.SelectItem != null)
                zw = (this.dtbPolitical.DataContext as JinShen_MainInfo).Position;
            this.epJbxx.Header = _Department + "【" + zw + "】";

            //带或的都是要判断的 就是如果选择了带或的 那么相应的 那边必须选择乡科级正职或副职
            if (zw.Contains("或"))
            {
                // entity.PositionLevel = "";
                //this.dtbPositionLevel.SelectValue = "乡科级正职或副职";
                this.dtbPositionLevel.Visibility = Visibility.Collapsed;
                this.dtbPositionLevel1.Visibility = Visibility.Visible;
                entity.PositionLevel = "乡科级正职或副职";
            }
            else
            {
                entity.PositionLevel = "";
                this.dtbPositionLevel.Visibility = Visibility.Visible;
                this.dtbPositionLevel1.Visibility = Visibility.Collapsed;
            }

        }
        private string _Department = "";
        private void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                entity.Department= _Department = ((BianZhiModel)((((ComboBox)sender).SelectedItem))).JiGouMingCheng;

               this.epJbxx.Header = entity.Department + "【" + zw + "】";
            }
        }
       
    }
}
